package com.yanqu.road.dao.impl.cross.banquet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.impl.TempNoPoolDao;
import com.yanqu.road.entity.banquet.cross.CrossBanquetMember;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossBanquetMemberNotPoolDaoImpl extends TempNoPoolDao {


    public CrossBanquetMemberNotPoolDaoImpl(String dbParam) {
        super(dbParam);
    }

    public Map<Long, CrossBanquetMember> getCrossBanquetMemberMap(String banquetId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_c_banquet_member where banquetId = ?";
        Map<Long, CrossBanquetMember> res = new ConcurrentHashMap<>();
        try {
            call = getConnection().prepareCall(sql);
            int i = 1;
            call.setString(i++,banquetId);

            rs = call.executeQuery();
            while (rs.next()) {
                CrossBanquetMember data = parseData(rs);
                res.put(data.getUserId(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            closeConnection(call, rs);
        }
        return res;
    }


    protected CrossBanquetMember parseData(ResultSet rs) throws SQLException {
        CrossBanquetMember data = new CrossBanquetMember();
        data.setBanquetId(rs.getString("banquetId"));
        data.setUserId(rs.getLong("userId"));
        data.setGiftId(rs.getInt("giftId"));
        data.setPopularity(rs.getLong("popularity"));
        data.setIntegral(rs.getLong("integral"));
        data.setPos(rs.getInt("pos"));
        data.setRobot(rs.getBoolean("robot"));
        data.setRobotProcessId(rs.getInt("robotProcessId"));
        data.setRobotId(rs.getInt("robotId"));
        String baseUserInfoStr = rs.getString("userBaseInfo");
        if(null == baseUserInfoStr){
            baseUserInfoStr = "";
        }
        UserBaseInfo userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
        if(null != userBaseInfo) {
            data.setUserBaseInfo(userBaseInfo);
        }
        return data;
    }
}
